package com.nhim.cms.admin.client.group.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nhim.cms.admin.client.rpc.ContentTypeRPCService;
import com.nhim.cms.admin.client.rpc.ContentTypeRPCServiceAsync;
import com.nhim.cms.admin.shared.ContentTypeRPCProxy;
import com.nhim.cms.admin.shared.GroupProxy;

public class GroupContentTypeSelection extends PopupPanel {

	private static GroupContentTypeSelectionUiBinder uiBinder = GWT.create(GroupContentTypeSelectionUiBinder.class);

	@UiTemplate("GroupContentTypeSelection.ui.xml")
	interface GroupContentTypeSelectionUiBinder extends
			UiBinder<Widget, GroupContentTypeSelection> {
	}
	
	/**
	 * Custom listener for this widget.
	 */
	public interface Listener {
		void onSave();
	}
	private Listener listener;
	public void setListener(Listener listener) {
		this.listener = listener;
	}

	@UiField
	Button btnSave;
	@UiField
	Button btnCancel;
	@UiField
	Button btnGraterThan;
	@UiField
	Button btnLessThan;
	@UiField
	ListBox ltAvailableProp;
	@UiField
	ListBox ltSelectedProp;
	 Map<Integer,String> selected = new HashMap<Integer,String>();
	 Map<Integer,String> available = new HashMap<Integer,String>();
	 GroupProxy selectedGroupProxy;
	private final ContentTypeRPCServiceAsync service = GWT.create(ContentTypeRPCService.class);
	public GroupContentTypeSelection(GroupProxy selectedGroupProxy) {
		super(false, true);
		add(uiBinder.createAndBindUi(this));
		this.selectedGroupProxy = selectedGroupProxy;
		ltSelectedProp.setVisibleItemCount(15);
		ltSelectedProp.setWidth("16em");
		ltAvailableProp.setVisibleItemCount(15);
		ltAvailableProp.setWidth("16em");
		selected.clear();
		if(selectedGroupProxy != null)
		{
			service.findByGroupId(selectedGroupProxy.getId(), new AsyncCallback<ArrayList<ContentTypeRPCProxy>>(){

				public void onFailure(Throwable e) {
					Window.alert("There was an error while fetching ContentTypes.");
					
				}

				public void onSuccess(ArrayList<ContentTypeRPCProxy> results) {
					for(ContentTypeRPCProxy proxy : results)
					{
						ltSelectedProp.addItem(proxy.getName(), proxy.getId().toString());
						selected.put(proxy.getId(),proxy.getName());
					}
					
				}});
		}
		service.findAll(new AsyncCallback<ArrayList<ContentTypeRPCProxy>>(){

			public void onFailure(Throwable errs) {
				
				Window.alert("Error:" + errs.getMessage());
			}

			public void onSuccess(ArrayList<ContentTypeRPCProxy> results) {
				for(ContentTypeRPCProxy ctProxy : results)
				{
					if(!selected.containsKey(ctProxy.getId()))
					{
						ltAvailableProp.addItem(ctProxy.getName(), ctProxy.getId().toString());
						available.put(ctProxy.getId(),ctProxy.getName());
					}
					
				}
				
			}});
		ltAvailableProp.addDoubleClickHandler(new DoubleClickHandler(){

			public void onDoubleClick(DoubleClickEvent arg0) {
				Integer transferId = Integer.parseInt(ltAvailableProp.getValue(ltAvailableProp.getSelectedIndex()));
				selected.put(transferId, available.get(transferId));
				available.remove(transferId);
				rebindList();
				
			}});
		ltSelectedProp.addDoubleClickHandler(new DoubleClickHandler(){

			public void onDoubleClick(DoubleClickEvent arg0) {
				Integer transferId = Integer.parseInt(ltSelectedProp.getValue(ltSelectedProp.getSelectedIndex()));
				available.put(transferId, selected.get(transferId));
				selected.remove(transferId);
				rebindList();
				
			}});
	}
	private void rebindList()
	{
		ltAvailableProp.clear();
		ltSelectedProp.clear();
		for(Map.Entry<Integer, String> entry : selected.entrySet())
		{
			ltSelectedProp.addItem(entry.getValue(), entry.getKey().toString());
		}
		for(Map.Entry<Integer, String> entry : available.entrySet())
		{
			ltAvailableProp.addItem(entry.getValue(), entry.getKey().toString());
		}
		
	}
	@UiHandler("btnCancel")
	void handleButtonCancelClick(ClickEvent e) {
		this.hide();

	}
	@UiHandler("btnSave")
	void handleButtonSaveClick(ClickEvent e) {
		Set<Integer> listContentType = new HashSet<Integer>();
		for(Map.Entry<Integer, String> entry : selected.entrySet())
		{
			listContentType.add(entry.getKey());
		}
		service.persitGroupContentTypes(selectedGroupProxy.getId(), listContentType, new AsyncCallback<Boolean>(){

			public void onFailure(Throwable arg0) {
				Window.alert("Error");
				
			}

			public void onSuccess(Boolean arg0) {
				if(listener!= null)
				{
					listener.onSave();
				}
				
			}});
		this.hide();

	}
	@UiHandler("btnGraterThan")
	void handleButtonGraterThanClick(ClickEvent e) {
		for(int i =0; i < ltAvailableProp.getItemCount();i++)
		{
			
			if(ltAvailableProp.isItemSelected(i))
			{
				Integer transferId = Integer.parseInt(ltAvailableProp.getValue(i));
				selected.put(transferId, available.get(transferId));
				available.remove(transferId);
			}		
		}
		rebindList();

	}
	@UiHandler("btnLessThan")
	void handleButtonLessThanClick(ClickEvent e) {
		for(int i =0; i < ltSelectedProp.getItemCount();i++)
		{
			
			if(ltSelectedProp.isItemSelected(i))
			{
				Integer transferId = Integer.parseInt(ltSelectedProp.getValue(i));
				available.put(transferId, selected.get(transferId));
				selected.remove(transferId);
			}		
		}
		rebindList();

	}

}
